const express = require('express');
const httpServer = express();

const Tasks = require('./task_info_changda');
const GetUser = require('./get_User_changda');
const address = require('./get_address_changda');
const evaluation = require('./User_evaluation_changda');
const Box_info = require('./box_Task_changda');
const Print_Info = require('./Print_Task_changda');
const Exp_evaluation = require('./express_evaluation_changda')


const Task_info = new Tasks.Task();
const get_user = new GetUser.Get_User();
const get_Address = new address.Address();
const User_eva = new evaluation.User_evaluation();
const BOX_info = new Box_info.Box_info();
const PRINT_info = new Print_Info.Print_info();
const express_eva = new Exp_evaluation.User_evaluation_Exp();

httpServer.all('*',function(req,res,next){
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
})


httpServer.get('/getTaskInfo',(req,res)=>{
    let message = Task_info.getInfo();
    res.send({code:0,message})
})

httpServer.get('/saveTask',(req,res)=>{
    console.log(req.query.User_address)
    console.log(req.query.express)
    let User_address = JSON.parse(req.query.User_address);  //将一个字符串转换成一个对象
    let express = JSON.parse(req.query.express)
    let message = Task_info.AddTask(User_address,express);
    res.send({code:0,message});
})


httpServer.get('/paoshou_OrderReceiving',(req,res)=>{
    // console.log(req.query.id);
    // console.log(req.query.number);
    let code = Task_info.paoshou_OrderReceiving(req.query.id,req.query.number);
    res.send({code});
})


// 删除已有的任务
httpServer.get('/remove_Task',(req,res)=>{
    // console.log(req.query.id);
    let flag = Task_info.remove_Task(req.query.id);
    // console.log(flag);
    if(flag == 0){
        code = 0;
    }else{
        code = 1;
    }
    res.send({code});
})


//用户确定收货
httpServer.get('/Finish_Task_order',(req,res)=>{
    // console.log(req.query.id);
    Task_info.Finished_Task_order(req.query.id);
    res.send({code:0});
})


//返回搜索框中搜索到的信息
httpServer.get('/get_SearchMessage',(req,res)=>{
    // console.log(req.query.message);
    let Arr = Task_info.Search_message(req.query.message);
    // if()
    // console.log(Arr,'1111111');
    res.send({code:0,Arr})
})


//跑手完成任务订单
httpServer.get('/finished_Task',(req,res)=>{
    Task_info.Finished_Task(req.query.id,req.query.number);
    res.send({code:0})
})




//获取登录用户信息
httpServer.get('/getUser',(req,res)=>{
    // console.log(req.query.number,req.query.secret);
    let message = get_user.getUser(req.query.number,req.query.secret);
    // console.log(message)
    res.send({code:0,message})
})

//注册成为跑手
httpServer.get('/revise_ifRunner',(req,res)=>{
    // console.log(req.query.number);
    // console.log(JSON.parse(req.query.message));
    let info = JSON.parse(req.query.message);
    let number = req.query.number;
    let message = get_user.revise_ifRunner(number,info);
    let code;
    if(message == -1){
        code = -1;
    }else{
        code = 0;
    }
    res.send({code,message});
})


//注册用户
httpServer.get('/register_User',(req,res)=>{
    // console.log(JSON.parse(req.query.New_user));
    let NewUser = JSON.parse(req.query.New_user);
    get_user.register_User(NewUser);
    res.send({code:0});
})


//修改用户信息
httpServer.get('/revise_User',(req,res)=>{
    get_user.revise_message(req.query.name,req.query.number);
    res.send({code:0});
})


//修改用户密码
httpServer.get('/revise_secret',(req,res)=>{
    // console.log(req.query.old_Secret,req.query.secret,req.query.number);
    let code = get_user.revise_secret(req.query.old_Secret,req.query.secret,req.query.number)
    res.send({code});
})


// 获得地址信息
httpServer.get('/getAddress',(req,res)=>{
    let message = get_Address.getAddress();
    res.send({code:0,message});
})


// 修改用户地址信息
httpServer.get('/revise_address',(req,res)=>{
    // console.log(JSON.parse(req.query.message));
    let message = JSON.parse(req.query.message);
    get_Address.revise_address(message);
    res.send({code:0})
})


// 新增用户地址信息
httpServer.get('/add_address',(req,res)=>{
    // console.log(JSON.parse(req.query.message));
    let message = JSON.parse(req.query.message);
    get_Address.add_address(message);
    res.send({code:0})
})


// 删除用户地址
httpServer.get('/remove_address',(req,res)=>{
    get_Address.remove_address(req.query.id,req.query.number);
    res.send({code:0})
})


//增添用户反馈
httpServer.get('/add_evaluation',(req,res)=>{
    // console.log();
    let message = JSON.parse(req.query.message);
    User_eva.add_evaluation(message)
    res.send({code:0});
})



//发布代盒饭任务
httpServer.get('/Add_boxTask',(req,res)=>{
    // console.log(JSON.parse(req.query.message));
    BOX_info.Add_boxTask(JSON.parse(req.query.message))
    res.send({code:0})
})

//获得任务信息
httpServer.get('/GetTaskMessage',(req,res)=>{
    let message = BOX_info.getTask();
    res.send({code:0,message})
})

//接手任务
httpServer.get('/selectTask',(req,res)=>{
    let code;
    let flag = BOX_info.select_Task(req.query.id,req.query.number)
    if(flag == 0){
        code = 0;
    }else{
        code = -1;
    }
    res.send({code})
})

//完成任务
httpServer.get('/Finish_BoxOrFood',(req,res)=>{
    // console.log(req.query.id,req.query.number);
    BOX_info.Finish_BoxOrFood(req.query.id,req.query.number);
    res.send({code:0})
})


//新增打印任务
httpServer.get('/Add_PrintTask',(req,res)=>{
    PRINT_info.Add_PrintTask(JSON.parse(req.query.message),JSON.parse(req.query.Info))
    res.send({code:0})
})


//获取打印任务信息
httpServer.get('/GetPrintMessage',(req,res)=>{
    let message = PRINT_info.GetPrintTask();
    res.send({code:0,message})
})

//接手打印任务
httpServer.get('/Get_PrintTask',(req,res)=>{
    // console.log(req.query.id,req.query.number,req.query.Select_number)
    PRINT_info.Get_PrintTask(req.query.id,req.query.number,req.query.Select_number)
    res.send({code:0})
})

//完成打印任务
httpServer.get('/Finish_PrintTask',(req,res)=>{
    PRINT_info.Finish_Task(req.query.id,req.query.number);
    res.send({code:0});
})


//用户评价（快递）
httpServer.get('/Get_exp_eva',(req,res)=>{
    // console.log(req.query.User,req.query.paoshou,req.query.Eval)
    express_eva.add_evaluation(req.query.User,req.query.paoshou,req.query.Eval,req.query.name)
    res.send({code:0});
})


//获得用户评价信息
httpServer.get('/GetEvalMessage',(req,res)=>{
    let message = express_eva.get_Message();
    res.send({code:0,message})
})


// httpServer.get('/info',(req,res)=>{
//     res.send({code:0})
// })

function start(){
    httpServer.listen(8080);
}
start(8080);